package com.it.glide.glidesample;

import android.util.Base64;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;

import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import okhttp3.ResponseBody;

public class WebServiceFetcher implements DataFetcher<InputStream> {
    public static final String WEBSERVICE_NAMESPACE = "http://impl.service.cxfandroid.yujing.gsafety.com/";

    private final GlideUrl url;
    private InputStream stream;
    private ResponseBody responseBody;
    private volatile boolean isCancelled;

    public WebServiceFetcher(GlideUrl model) {
        url=model;
    }


    @Override
    public InputStream loadData(Priority priority) throws Exception {
     /*   Request.Builder requestBuilder = new Request.Builder()
                .url(url.toStringUrl());
        for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
            String key = headerEntry.getKey();
            requestBuilder.addHeader(key, headerEntry.getValue());
        }
        requestBuilder.addHeader("httplib", "OkHttp");
        Request request = requestBuilder.build();
        if (isCancelled) {
            return null;
        }
        Response response = client.newCall(request).execute();
        responseBody = response.body();
        if (!response.isSuccessful() || responseBody == null) {
            throw new IOException("Request failed with code: " + response.code());
        }
        stream = ContentLengthInputStream.obtain(responseBody.byteStream(),
                responseBody.contentLength());*/
        String s = url.getCacheKey();
        String[] tempUrl = s.split("\\|");
       
            HashMap<String, String> map = new HashMap<String, String>();
				/*WebParams fromJson = new Gson().fromJson(tempUrl[3], WebParams.class);
				if(TextUtils.isEmpty(fromJson.warning_type_code)||TextUtils.isEmpty(fromJson.msg_level_code)){
					throw new Exception();
				}else{
					if("null".equals(fromJson.warning_type_code)||"null".equals(fromJson.msg_level_code)){
					throw new Exception();
					}

				}*/
            map.put("jsonParam", tempUrl[3]);
            WebServiceUtils.callSyncWebService(tempUrl[1], WEBSERVICE_NAMESPACE, tempUrl[2], map,
                    new WebServiceCallBack() {

                        @Override
                        public void onSuccess(String result) {
                            // TODO Auto-generated method stub
                            JSONObject jsonObject;
                            try {
                                jsonObject = new JSONObject(result);
                                if ("ok".equals(jsonObject.get("result"))) {
                                    String fileStr = (String) jsonObject.get("fileStr");
                                    byte[] decode = Base64.decode(fileStr, Base64.DEFAULT);
//										Base64.de
//                                    stream.decode(decode);
                                    // iStream = new

                                }
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onFailure(String reason) {
                            // TODO Auto-generated method stub


                        }
                    });



return stream;




    }

    @Override
    public void cleanup() {
        try {
            if (stream != null) {
                stream.close();
            }
            if (responseBody != null) {
                responseBody.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getId() {
        return url.getCacheKey();
    }

    @Override
    public void cancel() {
        isCancelled = true;
    }
}